package com.ruoyi.web.controller.report;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.report.vo.*;
import com.ruoyi.system.service.report.ITeacherService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/report/teacher")
public class TeacherReportController extends BaseController {

    @Autowired
    private ITeacherService iTeacherService;

    @Autowired
    private TokenService tokenService;

    /**
     * 分页条件查询
     *
     * @param tabPtdetailTailPatientVO
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "患者分页条件查询")
    public AjaxResult list(@RequestBody TabPtdetailTailPatientVO tabPtdetailTailPatientVO) {
        System.out.println(tabPtdetailTailPatientVO);
        return AjaxResult.success(iTeacherService.getList(tabPtdetailTailPatientVO));
    }

    /**
     * 检查项目下拉框
     *
     * @return
     */
    @GetMapping("/getDropDownCheckName")
    @ApiOperation(value = "获取下拉检查项目")
    public AjaxResult getDropDownCheckName() {
        List<String> list = iTeacherService.getDropDownCheckName();
        return AjaxResult.success(list);
    }

    /**
     * 下拉状态
     *
     * @return
     */
    @GetMapping("/getDropDownState")
    @ApiOperation(value = "获取下拉状态")
    public AjaxResult getDropDownState() {
        return AjaxResult.success(iTeacherService.getDropDownState());
    }

    /**
     * 获取患者随访记录
     *
     * @param ptdeId
     * @return
     */
    @GetMapping("/getSurvey/{ptdeId}")
    @ApiOperation(value = "获取随访记录")
    public AjaxResult getSurvey(@PathVariable String ptdeId) {
        return AjaxResult.success(iTeacherService.getSurvey(ptdeId));
    }


    /**
     * 查询病史
     *
     * @param ptdeId
     * @return
     */
    @GetMapping("/getBgReadyTxt/{ptdeId}")
    public AjaxResult getBgReadyTxt(@PathVariable String ptdeId, String reportId) {
        String userName = tokenService.getLoginUser(ServletUtils.getRequest()).getUsername();
        return AjaxResult.success(iTeacherService.getBgReadyTxt(ptdeId, userName, reportId));
    }

    /**
     * 检查意见
     *
     * @param patientId
     * @return
     */
    //@GetMapping("/getCheckyjTxt/{patientId}")
    public AjaxResult ptdtCheckyjTxt(@PathVariable String patientId) {
        return AjaxResult.success(iTeacherService.ptdtCheckyjTxt(patientId));
    }


    /**
     * 检查所见
     *
     * @param patientId
     * @return
     */
    //@GetMapping("/getCheckSjTxt/{patientId}")
    public AjaxResult ptdtCheckSjTxt(@PathVariable String patientId) {
        return AjaxResult.success(iTeacherService.ptdtCheckSjTxt(patientId));
    }


    /**
     * 患者查询
     *
     * @param
     * @return
     */
    @GetMapping("/getPatient/{ptdeId}")
    @ApiOperation(value = "学生患者查询")
    public AjaxResult getPatient(@PathVariable String ptdeId) {
        return AjaxResult.success(iTeacherService.getPatient(ptdeId));
    }

    /**
     * 更新患者信息
     *
     * @param ptPatientVo
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "更新患者信息")
    public AjaxResult updatePatient(@RequestBody PtPatientVo ptPatientVo) {
        iTeacherService.updatePatient(ptPatientVo);
        try {

        } catch (Exception e) {
            throw new GlobalException("保存失败");
        }
        return AjaxResult.success("保存成功", null);
    }


    /**
     * 获取历史记录
     *
     * @param
     * @return
     */
    @GetMapping("/getHistory/{patientId}/{ptdeId}")
    @ApiOperation(value = "获取历史记录")
    public AjaxResult getHistory(@PathVariable String patientId,@PathVariable String ptdeId) {
        return AjaxResult.success(iTeacherService.getHistory(patientId,ptdeId));
    }


    /**
     * 获取 病例,检查意见,检查所见
     *
     * @param
     * @return
     */
    @GetMapping("/patientReport/{ptdeId}")
    @ApiOperation(value = "获取 病史,检查意见,检查所见")
    public AjaxResult getPatientReportInfo(@PathVariable String ptdeId) {
        return AjaxResult.success("rows", iTeacherService.getPatientReportInfo(ptdeId));
    }

    /**
     * 序列
     *
     * @param checkCode
     * @return
     */
    @GetMapping("/series/{checkCode}")
    @ApiOperation(value = "序列")
    public AjaxResult getStudiesSeries(@PathVariable String checkCode) {
        return AjaxResult.success(iTeacherService.getStudiesSeries(checkCode));
    }


    /**
     * 查询模板
     *
     * @return
     */
    @GetMapping("/template")
    @ApiOperation(value = "模板")
    public AjaxResult getTemplate() {
        Long userId = tokenService.getLoginUser(ServletUtils.getRequest()).getUserId();
        return AjaxResult.success("publicLibrary", iTeacherService.getTemplate(userId));
    }

    /**
     * 检查意见检查所见模板
     *
     * @param id
     * @param parentId
     * @return
     */
    @GetMapping("/templateInfo/{id}/{parentId}")
    @ApiOperation(value = "模板")
    public AjaxResult templateInfo(@PathVariable String id, @PathVariable String parentId) {
        if (id == null || parentId == null) {
            return AjaxResult.error("查询失败");
        }
        return AjaxResult.success(iTeacherService.templateInfo(id, parentId));
    }


    /**
     * 发布患者
     *
     * @param
     * @return
     */
    @PostMapping("/release")
    public AjaxResult addRelease(@RequestBody List<ClassStudentListVo> classStudentListVo) {
        String teacherUserName = tokenService.getLoginUser(ServletUtils.getRequest()).getUsername();
        try {
            iTeacherService.addRelease(classStudentListVo, teacherUserName);
        } catch (Exception e) {
            throw new GlobalException("发布异常,请联系管理员");
        }
        return AjaxResult.success("发布成功");
    }

    /**
     * 获取当前用户管理的所有班级
     *
     * @return
     */
    @GetMapping("/classList")
    public AjaxResult getClassList() {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        return AjaxResult.success(iTeacherService.getClassList(loginUser.getUsername()));
    }

    /**
     * 查询班级内的所有学生
     *
     * @param classZ
     * @return
     */
    @GetMapping("/classStudentList/{class}")
    public AjaxResult getClassStudentList(@PathVariable("class") String classZ) {
        return AjaxResult.success(iTeacherService.getClassStudentList(classZ));
    }


    /**
     * 学生报告条件分页查询
     *
     * @param studentReportReqVo
     * @return
     */
    @PostMapping("/studentReportListPage")
    public TableDataInfo getStudentReportListPage(@RequestBody StudentReportReqVo studentReportReqVo) {
        String userName = tokenService.getLoginUser(ServletUtils.getRequest()).getUsername();
        startPage();
        List<StudentReportListPageResVo> studentReportListPage = iTeacherService.getStudentReportListPage(studentReportReqVo, userName);
        return getDataTable(studentReportListPage);
    }

    /**
     * 提交评价
     * @param commentScoreReqVo
     * @return
     */
    @PostMapping("/score/{status}")
    public AjaxResult addCommentScore(@RequestBody CommentScoreReqVo commentScoreReqVo,@PathVariable String status) {
        if (StringUtils.isEmpty(commentScoreReqVo.getScore())){
            throw new GlobalException("分数不能为空");
        }
        iTeacherService.addCommentScore(commentScoreReqVo,status);
        return AjaxResult.success("保存成功");
    }

    /**
     * 提交评价
     * @param commentScoreReqVo
     * @return
     */
    @PostMapping("update/score")
    public AjaxResult updateCommentScore(@RequestBody CommentScoreReqVo commentScoreReqVo) {
        if (StringUtils.isEmpty(commentScoreReqVo.getScore())){
            throw new GlobalException("分数不能为空");
        }
        iTeacherService.updateCommentScore(commentScoreReqVo);
        return AjaxResult.success("保存成功");
    }

    /**
     * 获取报告完成状态
     *
     * @param
     * @return
     */
    @GetMapping("completeStatus")
    public AjaxResult getCompleteStatus() {
        return AjaxResult.success(iTeacherService.getCompleteStatus());
    }


    /**
     * 评价学生报告
     *
     * @return
     */
    @GetMapping("commentReport/{reportId}")
    public AjaxResult getReportComment(@PathVariable String reportId) {
        return AjaxResult.success(iTeacherService.getReportComment(reportId));
    }
}
